// utils/seo.js
export const setMeta = (meta) => {
    const head = document.head
    const title = document.querySelector('title') || document.createElement('title')
    title.textContent = meta.title || 'CMS系统'
    head.appendChild(title)
    
    // 移除旧的meta标签
    const oldMeta = head.querySelectorAll('meta[name^="og:"], meta[name="description"], meta[name="keywords"]')
    oldMeta.forEach(el => head.removeChild(el))
    
    // 添加新的meta标签
    const metas = [
      { name: 'description', content: meta.description || '' },
      { name: 'keywords', content: meta.keywords || '' },
      { property: 'og:title', content: meta.title || '' },
      { property: 'og:description', content: meta.description || '' },
      { property: 'og:type', content: meta.type || 'website' },
      { property: 'og:url', content: meta.url || window.location.href },
      { property: 'og:image', content: meta.image || '/static/images/logo.png' }
    ]
    
    metas.forEach(config => {
      if (!config.content) return
      const tag = document.createElement('meta')
      Object.keys(config).forEach(key => {
        tag.setAttribute(key.startsWith('og:') ? 'property' : 'name', key)
        tag.setAttribute('content', config[key])
      })
      head.appendChild(tag)
    })
  }